package cn.yunyichina.provider.pay.service.impl;

import cn.yunyichina.provider.framework.common.ResponseEnum;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.pay.TradeRecordRequest;
import cn.yunyichina.provider.iface.entity.pay.TradeRecordResponse;
import cn.yunyichina.provider.pay.dao.TradeRecordDao;
import cn.yunyichina.provider.pay.service.TradeRecordService;
import cn.yunyichina.utils.convert.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Lullaby on 2017/2/11.
 */
@Service
public class TradeRecordServiceImpl implements TradeRecordService {

    private static final Logger logger = LoggerFactory.getLogger(TradeRecordServiceImpl.class);

    @Resource
    private TradeRecordDao tradeRecordDao;

    public ResponseObject queryTradeRecord(String params) {
        logger.info("TradeRecordServiceImpl.queryTradeRecord.params -> {}", params);
        TradeRecordRequest tradeRecordRequest = JsonUtils.parseObject(params, TradeRecordRequest.class);

        ResponseObject response = new ResponseObject();

        if (checkQueryTradeRecordParameters(tradeRecordRequest)) {
            List<TradeRecordResponse> recordDaoList = tradeRecordDao.queryTradeRecord(tradeRecordRequest);

            if (!CollectionUtils.isEmpty(recordDaoList)) {
                response.setResultObject(recordDaoList);
            }
        } else {
            response.setResultCodeAndMessage(
                    ResponseEnum.Common.INVALID.getCode(),
                    ResponseEnum.Common.INVALID.getMessage()
            );
        }

        return response;
    }

    private boolean checkQueryTradeRecordParameters(TradeRecordRequest request) {
        if (StringUtils.isEmpty(request.getAppid()) ||
                StringUtils.isEmpty(request.getOrderType()) ||
                StringUtils.isEmpty(request.getTradeType()) ||
                StringUtils.isEmpty(request.getStartTime()) ||
                StringUtils.isEmpty(request.getEndTime())) {
            return false;
        }

        return true;
    }

}
